package org.jeecg.modules.rental.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.modules.rental.cache.ScenicSpotCache;
import org.jeecg.modules.rental.entity.RentalScenicSpot;
import org.jeecg.modules.rental.mapper.RentalScenicSpotMapper;
import org.jeecg.modules.rental.service.IRentalScenicSpotService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 区域景点
 * @Author: jeecg-boot
 * @Date:   2024-07-13
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class RentalScenicSpotServiceImpl extends ServiceImpl<RentalScenicSpotMapper, RentalScenicSpot> implements IRentalScenicSpotService {

    private final CacheManager cacheManager;

    private final ScenicSpotCache scenicSpotCache;

    @Override
    public IPage<RentalScenicSpot> selectJoinPage(Page page, RentalScenicSpot rentalScenicSpot) {
        IPage<RentalScenicSpot> pageList = baseMapper.selectJoinPage(page, rentalScenicSpot);
        if(ObjectUtil.isNotEmpty(pageList.getRecords())){
            Cache deptCache = cacheManager.getCache(CacheConstant.SYS_DEPARTS_CACHE);
            pageList.getRecords().stream().forEach(item->{
                if(ObjectUtil.isNotEmpty(deptCache)){
                    //TODO 优化部门缓存
//                    SysDepartVO sysDepartVO = deptCache.get(item.getSysOrgCode(), SysDepartVO.class);
//                    if(ObjectUtil.isNotEmpty(sysDepartVO)){
//                        item.setSysOrgName(sysDepartVO.getDepartName());
//                    }
                }
            });
        }
        return pageList;
    }

    @Override
    public List<RentalScenicSpot> selectOptions(RentalScenicSpot rentalScenicSpot) {
        List<RentalScenicSpot> optionList = baseMapper.selectOptions(rentalScenicSpot);
        return optionList;
    }

    @Override
    public List<RentalScenicSpot> queryByLocation(String latitude, String longitude) {
        //通过经纬度获取最近的区域
        List<RentalScenicSpot> scenicSpotList = baseMapper.selectList(
                new QueryWrapper<RentalScenicSpot>());
        scenicSpotList.stream().forEach(item->{
            if(StrUtil.isNotBlank(item.getIntro())){
                Document doc = Jsoup.parse(item.getIntro());
                // 创建一个列表来存储段落或div的纯文本内容
                List<String> textBlocks = new ArrayList<>();
                // 选择所有的<p>和<div>元素
                Elements paragraphsAndDivs = doc.select("p, div");
                // 遍历这些元素，并将它们的文本内容添加到列表中
                for (Element element : paragraphsAndDivs) {
                    String text = element.text().replaceAll("\\s+", "");
                    if(StrUtil.isNotBlank(text)){
                        textBlocks.add(text); // 使用.text()获取纯文本内容
                    }
                }
                item.setIntroTxt(textBlocks);
            }
        });
        return scenicSpotList;
    }

    @Override
    public Boolean add(RentalScenicSpot rentalScenicSpot) {
        Boolean result = this.save(rentalScenicSpot);
        scenicSpotCache.addLocation(
                new Point(Double.parseDouble(rentalScenicSpot.getLongitude()) , Double.parseDouble(rentalScenicSpot.getLatitude())),
                rentalScenicSpot.getId().toString());
        return result;
    }

    @Override
    public Boolean edit(RentalScenicSpot rentalScenicSpot) {
        scenicSpotCache.addLocation(
                new Point(Double.parseDouble(rentalScenicSpot.getLongitude()) , Double.parseDouble(rentalScenicSpot.getLatitude())),
                rentalScenicSpot.getId().toString());
        return this.updateById(rentalScenicSpot);
    }

    @Override
    public void initCacheLocation() {
        List<RentalScenicSpot> rentalList = this.list(new QueryWrapper<RentalScenicSpot>().isNotNull("latitude").isNotNull("longitude"));
        scenicSpotCache.initLocation(rentalList);
    }
}
